
#include <wx/defs.h>
#include <wx/mimetype.h>
#include <wx/fontdlg.h>
#include <wx/filename.h>

#include "GlobalConfig.h"
#include "UserConfig.h"

#include "MsnCommand.h"
#include "MsnContact.h"

#include "MsnMessenger.h"

#include "MsnSession.h"
#include "MsnEvent.h"

#include "MsnConnection.h"
#include "ServerConnection.h"
#include "SessionConnection.h"

#include "ContactManager.h"
#include "SessionManager.h"


BEGIN_EVENT_TABLE(MsnMessenger, wxEvtHandler)


END_EVENT_TABLE()


MsnMessenger::MsnMessenger()
	:totalCount(0),msnEventListener(0),
	serverConnection(0),contactManager(0),userConfig(0),user(0), messengerStatus(MESSENGER_NEW)
{

	sessionManager = new SessionManager(this);

    //this->Connect(wxEVT_MSN_SESSION_CLOSE, (wxObjectEventFunction)&MsnMessenger::OnMsnSessionColsed);
}

MsnMessenger::~MsnMessenger()
{
	//delete all sessions
	if(sessionManager){
		sessionManager->Clear();
		delete sessionManager;
		sessionManager = NULL;
	}

	Reset();

	DeleteServerConnection();

	DEBUG(_T("~MsnMessenger"));
}

void MsnMessenger::PostEvent(wxEvent& event)
{
	if(msnEventListener)
		msnEventListener->AddPendingEvent(event);
}

bool MsnMessenger::IsAlive()
{
	return IsConnected() && user != NULL;
}

bool MsnMessenger::IsConnected()
{
    return serverConnection!=NULL && serverConnection->IsConnected();
}

bool MsnMessenger::IsBehindWall()
{
	return !internalIP.IsSameAs(externalIP);
}

wxString MsnMessenger::GetDisplayName(const wxString& email)
{
	if(email.IsSameAs(userPassport)){
		return user->GetName();
	}else{
		MsnContact* contact = contactManager->GetContact(email);
		if(contact != NULL)
			return contact->GetDisplayName();
		return email;
	}
}

wxString MsnMessenger::GetDisplayPicture()
{
	wxString picFile = GetPictureDirectory() + _T("/") + userConfig->GetDisplayPicture();

	if(wxFileName(picFile).FileExists()){
		return picFile;
	}else{
		return _T("tuxitty.jpg");
	}
}


wxString MsnMessenger::GetHomeDirectory()
{
	return SystemUtil::GetUserDataDir() + wxString(wxFileName::GetPathSeparator()) + userPassport;
}

wxString MsnMessenger::GetHistoryDirectory()
{
	if(userConfig->GetHistoryFolder().IsEmpty()){
		return GetHomeDirectory() + wxString(wxFileName::GetPathSeparator()) + _T("logs");
	}
	return userConfig->GetHistoryFolder();
}

wxString MsnMessenger::GetPictureDirectory()
{
	return GetHomeDirectory() + wxString(wxFileName::GetPathSeparator()) + _T("pics");
}

wxString MsnMessenger::GetFileTransferDirectory()
{
	if(userConfig->GetFileTransferFolder().IsEmpty()){
		return GetHomeDirectory() + wxString(wxFileName::GetPathSeparator()) + _T("files");
	}
	return userConfig->GetFileTransferFolder();
}

void MsnMessenger::DeleteServerConnection()
{
	if(serverConnection){
		serverConnection->Destroy();
		wxDELETE(serverConnection);
	}
}

void MsnMessenger::Reset()
{
	//DeleteServerConnection();

	wxDELETE(user);

	wxDELETE(contactManager);

	wxDELETE(userConfig);
}


void MsnMessenger::SignIn(const wxString& passport, const wxString& password, const wxString& status)
{
	messengerStatus = MESSENGER_CONNECTING;

	userPassport = passport;
    initialStatus = status;

	DeleteServerConnection();

	serverConnection = new ServerConnection(this);
    serverConnection->SetUserPassport(passport);
    serverConnection->SetUserPassword(password);

	int connectionType = GlobalConfig::GetConnectionType();
	if(connectionType == -1){
		connectionType = MSN_CONNECTION_DIRECT;
	}

	serverConnection->SetConnectionType(connectionType);

	wxString serverAddress = GlobalConfig::GetRecentServer();

	if(connectionType == MSN_CONNECTION_DIRECT){
		if(serverAddress.IsEmpty()){
			serverAddress = MSN_TCP_SERVER_ADDRESS;
		}
	}else{
		serverAddress = MSN_HTTP_SERVER_ADDRESS;	
	}

	serverConnection->SetServerAddress(serverAddress);

	if(connectionType == MSN_CONNECTION_HTTP_PROXY){
		wxString proxyAddress = GlobalConfig::GetHttpProxyAddress();

		DEBUG(_T("using http proxy: ") + proxyAddress);

		serverConnection->SetProxyAddress(proxyAddress);
		serverConnection->SetProxyPort(GlobalConfig::GetHttpProxyPort());
		serverConnection->SetProxyUsername(GlobalConfig::GetHttpProxyUsername());
		serverConnection->SetProxyPassword(GlobalConfig::GetHttpProxyPassword());		
	}

	serverConnection->Open();
}

void MsnMessenger::OnSignedIn(const wxString& passport)
{
	messengerStatus = MESSENGER_SIGNED_IN;

    FileUtil::MkDir(GetHomeDirectory());

    userConfig = new UserConfig(GetHomeDirectory()+_T("/settings"));

	FileUtil::MkDir(GetHistoryDirectory());
	FileUtil::MkDir(GetPictureDirectory());
	FileUtil::MkDir(GetFileTransferDirectory());

	user = new MsnContact();
	user->SetPassport(passport);
	user->SetName(userConfig->GetDisplayName());

	//set the default history folder
	if(userConfig->GetHistoryFolder().IsEmpty()){
		userConfig->SetHistoryFolder(GetHistoryDirectory());
	}

	//sync contact list
	contactManager = new ContactManager(GetHomeDirectory());
    SynchronizeContacts();

	//ChangeStatus(initialStatus);

	//notify UI
	wxCommandEvent event(EVT_MSN_SERVER_SIGNED_IN, -1);
	event.SetClientData(user);
	PostEvent(event);	

	GlobalConfig::SetRecentUser(userPassport);
    GlobalConfig::SetRecentServer(serverConnection->GetServerAddress());
}


void MsnMessenger::SignOut()
{
	///todo UI event
	messengerStatus = MESSENGER_SIGNED_OUT;
	
	DeleteServerConnection();

	Reset();
}

void MsnMessenger::OnSignedOut()
{
	DEBUG(_T("signed out event"));

	messengerStatus = MESSENGER_SIGNED_OUT;

	Reset();

    ///todo close all sessions

	wxCommandEvent event(EVT_MSN_SERVER_SIGNED_OUT, -1);
	PostEvent(event);
}

void MsnMessenger::OnConnectionFailed()
{
	DEBUG(_T("connection failed event"));

	messengerStatus = MESSENGER_CONNECTION_FAILED;

	Reset();

	wxCommandEvent event(EVT_MSN_SERVER_CONNECTION_FAILED, -1);
	PostEvent(event);
}

void MsnMessenger::OnConnectionBroken()
{
	DEBUG(_T("connection broken event"));

	messengerStatus = MESSENGER_CONNECTION_BROKEN;

	Reset();
    ///todo close all sessions

	wxCommandEvent event(EVT_MSN_SERVER_CONNECTION_BROKEN, -1);
	PostEvent(event);
}

void MsnMessenger::OnAuthenticationFailed()
{
	DEBUG(_T("authentication failed"));

	//todo 
	messengerStatus = MESSENGER_AUTHENTICATION_FAILED;

	wxCommandEvent event(EVT_MSN_SERVER_AUTHETICATION_FAILED, -1);
	PostEvent(event);	
}

void MsnMessenger::OnProfileMessageReceived(ProfileMessage* msg)
{
	DEBUG(_T("profile message received"));

	externalIP = msg->GetProperty(_T("ClientIP"));
	internalIP = SystemUtil::GetLocalAddress();

	if(!internalIP.IsSameAs(externalIP)){
		DEBUG(_T("messenger is behind wall."));
	}

}

void MsnMessenger::CheckSynchronization()
{
	totalCount--;
	if(totalCount == 0){
		DEBUG(_T("contact synchronization finished"));
		contactManager->SaveContact();
		contactManager->LoadContactEx();

		wxCommandEvent event(EVT_MSN_SYNCHRONIZATION_FINISHED, -1);
		PostEvent(event);
	}
}

void MsnMessenger::SynchronizeContacts()
{
    OutCommandSYN syn;
	syn.SetVersion(userConfig->GetContactVersion());
    serverConnection->SendCommand(syn);
}

void MsnMessenger::OnSynchronizationReceived(const wxString& newVersion, int groupCount, int contactCount)
{
	DEBUG(_T("OnSynchronizationReceived"));

	wxString savedVersion = userConfig->GetContactVersion();

    if(newVersion.IsSameAs(savedVersion)){
        DEBUG(_T("version is up to date..."));
		//contactManager->LoadGroup();
		contactManager->LoadContact();
		contactManager->LoadContactEx();

        wxCommandEvent event(EVT_MSN_SYNCHRONIZATION_FINISHED, -1);
		PostEvent(event);
    }else{
        DEBUG(_T("version is out of date..."));
        ///todo delete the cached files
        userConfig->SetContactVersion(newVersion);
        totalCount = groupCount + contactCount;
    }

	this->ChangeStatus(initialStatus);
}

void MsnMessenger::OnGroupReceived(MsnGroup* group)
{
	//add to contact manager
	contactManager->AddGroup(group);

    CheckSynchronization();
}

void MsnMessenger::OnContactReceived(MsnContact* contact)
{	
	//add to contact manager
	contactManager->AddContact(contact);

	CheckSynchronization();
}

void MsnMessenger::ChangeStatus(const wxString& status)
{
    OutCommandCHG chg;
    chg.SetStatus(status);

    ///todo set client id
	//int clientID = 0x10000000;
	int clientID = 0x4000C024;

	chg.SetClientID(wxString::Format(_T("%d"), clientID));


	wxString picFile = GetDisplayPicture();

	wxString msnObject = MsnUtil::CreateMsnObject(wxFileName(picFile), userPassport, 3);
	//wxString msnObject = _T("<msnobj Creator=\"tuxitty2@hotmail.com\" Type=\"3\" SHA1D=\"KkGt9kR7ZH12hKE1yelLR1h7uwc=\" Size=\"2039\" Location=\"0\" Friendly=\"egB3AG8AMwBfADAAMgBfAFAAZQByAGYAZQBjAHQAXwBzAGkAZABlAAAA\"/>");
	//wxString msnObject = _T("<msnobj Creator=\"tuxitty2@hotmail.com\" Type=\"3\" SHA1D=\"KkGt9kR7ZH12hKE1yelLR1h7uwc=\" Size=\"2039\" Location=\"0\" Friendly=\"AAA=\"/>");

	//<msnobj Creator="tuxitty@hotmail.com" Type="3" SHA1D="KkGt9kR7ZH12hKE1yelLR1h7uwc=" Size="2039" Location="0" Friendly="AAA="/>
	//<msnobj Creator="tuxitty@hotmail.com" Type="3" SHA1D="4nzvPk1cK9FFB7qqDdLIqm4o0cs=" Size="2141" Location="0" Friendly="AAA="/>

	chg.SetMsnObject(msnObject);

    serverConnection->SendCommand(chg);
}

void MsnMessenger::ChangeDisplayPicture(wxImage& image)
{
	wxString tempFile = GetPictureDirectory() + _T("/temp.jpg"); 
	image.SaveFile(tempFile, wxBITMAP_TYPE_JPEG);

	wxString sha1d = MsnUtil::Sha1D(wxFileName(tempFile));

	DEBUG(sha1d);

	userConfig->SetDisplayPicture(sha1d + _T(".jpg"));

	wxString picFile = GetPictureDirectory() + _T("/") + sha1d + _T(".jpg"); 
	wxRenameFile(tempFile, picFile);

    OutCommandCHG chg;
    chg.SetStatus(user->GetStatus());

    ///todo set client id
	//int clientID = 0x10000000;
	int clientID = 0x4000C024;

	chg.SetClientID(wxString::Format(_T("%d"), clientID));

	wxString msnObject = MsnUtil::CreateMsnObject(wxFileName(picFile), userPassport, 3);
	
	chg.SetMsnObject(msnObject);

    serverConnection->SendCommand(chg);	
}


void MsnMessenger::OnUserStatusChanged(const wxString& code)
{
	DEBUG(_T("on status changed"));

	//todo change the icon
	user->SetStatus(code);
    
	///todo UI event
	wxCommandEvent event(EVT_MSN_USER_STATUS_CHANGED, -1);
	event.SetString(code);
	PostEvent(event);
}

void MsnMessenger::ChangeStatusToIdle(bool isIdle)
{
	if(isIdle){
		ChangeStatus(_T("IDL"));
	}else{
		ChangeStatus(_T("NLN"));
	}	

}


void MsnMessenger::ChangeName(const wxString& name)
{
    OutCommandPRP prp;
	if(!name.IsEmpty()){
        prp.SetName(name);
	}else{
        prp.SetName(userPassport);
	}

    serverConnection->SendCommand(prp);
}

void MsnMessenger::OnUserNameReceived(const wxString& name)
{
	user->SetName(name);
	userConfig->SetDisplayName(name);

	wxCommandEvent event(EVT_MSN_USER_NAME_CHANGED, -1);
	event.SetString(name);
	PostEvent(event);
}


void MsnMessenger::OnContactNameChanged(const wxString& passport, const wxString& name)
{
	DEBUG(_T("on contact name changed: ") + name);
	
	contactManager->UpdateContactName(passport, name);

	MsnContact* contact = contactManager->GetContact(passport);

	wxCommandEvent event(EVT_MSN_CONTACT_NAME_CHANGED, -1);
	event.SetClientData(contact);
	PostEvent(event);
}

void MsnMessenger::OnContactStatusChanged(const wxString& passport, const wxString& status)
{
	DEBUG(_T("on contact status changed: ") + status);
	
	MsnContact* contact = contactManager->GetContact(passport);
	contact->SetStatus(status);

	//TODO close session

	wxCommandEvent event(EVT_MSN_CONTACT_STATUS_CHANGED, -1);
	event.SetClientData(contact);
	PostEvent(event);
}

void MsnMessenger::OnContactObjectChanged(const wxString& passport, const wxString& msnObject)
{

	//TODO

	MsnContact* contact = contactManager->GetContact(passport);
	contact->SetMsnObject(msnObject);

}

void MsnMessenger::AddContact(const wxString& passport, const wxString& list)
{
    OutCommandADC adc;
    adc.SetList(list);
    adc.SetPassport(passport);
    serverConnection->SendCommand(adc);   
}

void MsnMessenger::AddContactToForwardList(const wxString& passport)
{
    OutCommandADC adc;
    adc.SetList(_T("FL"));
    adc.SetPassport(passport);
	adc.SetName(passport);
    serverConnection->SendCommand(adc); 
}

void MsnMessenger::AddContactToAllowedList(const wxString& passport)
{
    OutCommandADC adc;
    adc.SetList(_T("AL"));
    adc.SetPassport(passport);
    serverConnection->SendCommand(adc); 
}

void MsnMessenger::OnContactAdded(MsnContact* contact)
{
    DEBUG(_T(" on contact added...") + contact->GetID());
   
	contactManager->AddContact(contact);

	wxCommandEvent event(EVT_MSN_CONTACT_ADDED, -1);
	event.SetClientData(contact);
	PostEvent(event);
}

void MsnMessenger::AddContactToGroup(const wxString& contactID, const wxString& groupID)
{
    OutCommandADC adc;
    adc.SetList(_T("FL"));
    adc.SetContactID(contactID);
    adc.SetGroupID(groupID);
    serverConnection->SendCommand(adc);
}


void MsnMessenger::OnContactAddedToGroup(const wxString& contactID, const wxString& groupID)
{
    DEBUG(_T(" onContactAdded to group...") + groupID);

	MsnContact* contact = contactManager->GetContactByID(contactID);

	if(contact && contact->GetGroupList().Count() == 0){
		DEBUG(_T("remove contact from root group "));
		wxCommandEvent event(EVT_MSN_CONTACT_REMOVED_FROM_GROUP, -1);
		event.SetClientData(contact);
		PostEvent(event);
    }

	contactManager->AddContactToGroup(contactID, groupID);
    
	wxCommandEvent event(EVT_MSN_CONTACT_ADDED_TO_GROUP, -1);
	event.SetClientData(contact);
	event.SetString(groupID);
	PostEvent(event);
}

void MsnMessenger::RemoveContact(const wxString& contactID, const wxString& groupID)
{
    OutCommandREM rem;
    rem.SetList(_T("FL"));
    rem.SetContactID(contactID);
    rem.SetGroupID(groupID);
    serverConnection->SendCommand(rem);
}


void MsnMessenger::DeleteContact(MsnContact* contact)
{
    OutCommandREM rem;
    rem.SetList(_T("FL"));
	rem.SetContactID(contact->GetID());
    serverConnection->SendCommand(rem);

    OutCommandREM rem2;
    rem2.SetList(_T("AL"));
	rem2.SetContactID(contact->GetPassport());
    serverConnection->SendCommand(rem2);
}

void MsnMessenger::BlockContact(const wxString& passport)
{
	AddContact(passport, _T("BL"));
}

void MsnMessenger::UnBlockContact(const wxString& passport)
{
    OutCommandREM rem;
    rem.SetList(_T("BL"));
	rem.SetContactID(passport);
    serverConnection->SendCommand(rem);
}

void MsnMessenger::OnContactAddedToForwardList(MsnContact* contact)
{
	DEBUG(_T(" OnContactAdded to forward list"));
	MsnContact* oldContact = contactManager->GetContact(contact->GetPassport());

	if(oldContact != NULL){
		DEBUG(_T("old contact existing..........."));
		contact->SetListNumber(oldContact->GetListNumber());
	}

	contact->AddList(FORWARD);
	contactManager->AddContact(contact);
	
	//notify tree ctrl
	wxCommandEvent event(EVT_MSN_CONTACT_ADDED, -1);
	event.SetClientData(contact);
	PostEvent(event);
}

void MsnMessenger::OnContactAddedToForwardList(const wxString& passport, const wxString& contactID, const wxString& name)
{
	DEBUG(_T(" OnContactAdded to forward list"));
	MsnContact* contact = contactManager->GetContact(passport);	

	if(contact == NULL){
		DEBUG(_T("create new contact"));

		contact = new MsnContact();
        contact->SetPassport(passport);
        contact->SetName(name);

		contactManager->AddContact(contact);
	}

	contact->SetID(contactID);
	contact->AddList(FORWARD);
	
	
	//notify tree ctrl
	wxCommandEvent event(EVT_MSN_CONTACT_ADDED, -1);
	event.SetClientData(contact);
	PostEvent(event);	

}

void MsnMessenger::OnContactAddedToAllowedList(const wxString& passport)
{
	DEBUG(_T(" OnContactAdded to allowed list"));

	MsnContact* contact = contactManager->GetContact(passport);
	if(contact == NULL){
		contact = new MsnContact();
		contact->SetPassport(passport);
		contact->SetListNumber(ALLOWED);
		contactManager->AddContact(contact);
	}else{
		DEBUG(_T("contact existing..........."));
		contact->AddList(ALLOWED);
	}	
}

void MsnMessenger::OnContactAddedToBlockedList(const wxString& passport)
{
	DEBUG(_T(" OnContactAdded to blocked list"));

	MsnContact* contact = contactManager->GetContact(passport);
	if(contact == NULL){
		contact = new MsnContact();
		contact->SetPassport(passport);
		contact->SetListNumber(BLOCKED);
		contactManager->AddContact(contact);
	}else{
		DEBUG(_T("contact existing..........."));
		contact->AddList(BLOCKED);
	}	

	//TODO notify UI to change icon
	wxCommandEvent event(EVT_MSN_CONTACT_ADDED_TO_BLOCKED_LIST, -1);
	event.SetClientData(contact);
	PostEvent(event);
}

//RL
void MsnMessenger::OnContactAddedToReversedList(const wxString& passport, const wxString& name)
{
	DEBUG(_T(" OnContactAdded to reversed list: "));

	MsnContact* contact = contactManager->GetContact(passport);
	if(contact == NULL){
		DEBUG(name + _T("added you as a friend"));
		//TODO notify user that someone added him to friend list
		contact = new MsnContact();
		contact->SetPassport(passport);
		contact->SetName(name);
		contact->SetListNumber(REVERSED);
		contactManager->AddContact(contact);

		wxCommandEvent event(EVT_MSN_CONTACT_ADDED_TO_REVERSED_LIST, -1);
		event.SetClientData(contact);
		PostEvent(event);

	}else{
		DEBUG(_T(" contact existing..........."));
		contact->AddList(REVERSED);
	}
}

void MsnMessenger::OnContactRemovedFromGroup(const wxString& contactID, const wxString& groupID)
{
	DEBUG(_T("OnContactRemovedFromGroup ")+contactID + _T(" from group ") + groupID);

    contactManager->RemoveContactFromGroup(contactID, groupID);

	MsnContact* contact = contactManager->GetContactByID(contactID);

	wxCommandEvent event(EVT_MSN_CONTACT_REMOVED_FROM_GROUP, -1);
	event.SetClientData(contact);
	event.SetString(groupID);
	PostEvent(event);

    
    if(contact && contact->GetGroupList().Count() == 0){
		DEBUG(_T("add contact to root group"));
		wxCommandEvent event(EVT_MSN_CONTACT_ADDED_TO_GROUP, -1);
		event.SetClientData(contact);
		PostEvent(event);
    }
}

void MsnMessenger::OnContactRemovedFromForwardList(const wxString& contactID)
{
	DEBUG(_T(" OnContactRemoved from forward list"));
	MsnContact* oldContact = contactManager->GetContactByID(contactID);

	if(oldContact == NULL){
		DEBUG(_T("contact not existing..........."));
		return;
	}

	oldContact->RemoveList(FORWARD);
	
	//notify tree ctrl
	wxCommandEvent event(EVT_MSN_CONTACT_REMOVED, -1);
	event.SetClientData(oldContact);
	PostEvent(event);
}

void MsnMessenger::OnContactRemovedFromAllowedList(const wxString& passport)
{
	DEBUG(_T(" OnContactRemoved from forward list"));
	MsnContact* oldContact = contactManager->GetContact(passport);

	if(oldContact != NULL){	
		oldContact->RemoveList(ALLOWED);
	}else{
		DEBUG(_T("contact not exists"));
	}
}

void MsnMessenger::OnContactRemovedFromBlockedList(const wxString& passport)
{
	DEBUG(_T(" OnContactRemoved from blocked list"));
	MsnContact* oldContact = contactManager->GetContact(passport);

	if(oldContact != NULL){	
		oldContact->RemoveList(BLOCKED);
		//TODO notify UI to change icon
		wxCommandEvent event(EVT_MSN_CONTACT_REMOVED_FROM_BLOCKED_LIST, -1);
		event.SetClientData(oldContact);
		PostEvent(event);

	}else{
		DEBUG(_T("contact not exists"));
	}
}

void MsnMessenger::OnContactRemovedFromReversedList(const wxString& passport)
{
	DEBUG(_T(" OnContactRemoved from reversed list"));
	MsnContact* oldContact = contactManager->GetContact(passport);

	if(oldContact != NULL){	
		oldContact->RemoveList(REVERSED);
		//TODO notify UI to change icon
	}else{
		DEBUG(_T("contact not exists"));
	}
}

void MsnMessenger::AddGroup(const wxString& name)
{
    OutCommandADG adg;
    adg.SetGroupName(name);
    serverConnection->SendCommand(adg);
}

void MsnMessenger::DeleteGroup(const wxString& groupID)
{
    OutCommandRMG rmg;
    rmg.SetGroupID(groupID);
    serverConnection->SendCommand(rmg);
}

void MsnMessenger::RenameGroup(const wxString& groupID, const wxString& newName)
{
    OutCommandREG cmd;
    cmd.SetGroupID(groupID);
    cmd.SetGroupName(newName);
    serverConnection->SendCommand(cmd);
}


void MsnMessenger::OnGroupAdded(const wxString& groupID, const wxString& groupName)
{
	MsnGroup* group = new MsnGroup(groupID, groupName);
    contactManager->AddGroup(group);

	wxCommandEvent event(EVT_MSN_GROUP_ADDED, -1);
	event.SetClientData(group);
	PostEvent(event);
}

void MsnMessenger::OnGroupRemoved(const wxString& groupID)
{
	contactManager->DeleteGroup(groupID);

	wxCommandEvent event(EVT_MSN_GROUP_REMOVED, -1);
	event.SetString(groupID);
	PostEvent(event);
}

void MsnMessenger::OnGroupRenamed(const wxString& groupID, const wxString& groupName)
{
	contactManager->UpdateGroupName(groupID, groupName);

	wxCommandEvent event(EVT_MSN_GROUP_RENAMED, -1);
	event.SetString(groupID);
	PostEvent(event);
}

void MsnMessenger::Ping()
{
    if(!serverConnection->IsConnected()) return;

    time_t now = wxDateTime::GetTimeNow();
    time_t last = serverConnection->GetLastActive();

    if(now - last > 300){
        OutCommandPNG png;
        serverConnection->SendCommand(png);
    }
}

int MsnMessenger::StartSession()
{
	return serverConnection->OpenSessionConnection();
}


void MsnMessenger::OpenLogDirectory()
{
    wxExecute(_T("explorer ") + GetHistoryDirectory(), wxEXEC_ASYNC,NULL);
}

void MsnMessenger::OpenFileDirectory()
{
    wxExecute(_T("explorer ") + GetFileTransferDirectory(), wxEXEC_ASYNC,NULL);
}
